package com.tedu;

import com.tedu.mapper.UserMapper;
import com.tedu.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/**
 * mybatis入门案例
 * 查询全部用户信息
 */
public class MybatisTest {
    public static void main(String[] args) throws IOException {
        //加载mybatis配置文件
        InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

        //获取sqlsession对象
        SqlSession sqlSession = factory.openSession();

        //执行mapper接口方法-----完成对数据库操作
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //获取mybatis生成的mapper接口实现类对象

        List<User> all = mapper.getAll();
        System.out.println(all);

        //下面  模拟用户的登录操作
        User user = new User();
        user.setUsername("lisi");
        user.setPassword("e12121");
        //调用mapper接口  进行登录
        List<User> login = mapper.login(user);
        System.out.println("登录成功后的用户信息："+login);

        System.out.println(mapper.getUser(32));


        //  #  $  取值区别  查询
//        System.out.println(mapper.getUserByCreate("created"));

        int i = mapper.deleteById(14);
        //提交事务
        sqlSession.commit();
        System.out.println(i);

        HashMap<String,String> map = new HashMap<>();
        map.put("id","16");
        map.put("password","123456");

        mapper.updatepwd(map);
        sqlSession.commit();


//        mapper.insert(all.get(3));

    }

}
